<?php
	/**
	 * @authors		sachin,shine,jonia
	 * @copyright	CMM Technologies
	 * @date        15-11-2011
	 * @file		Task.cls.php
	 * @description comman Task related functions are handled here.
	 */

	 class Task extends Base
	 {
		 /**
		 * Task id
		 * @var integer
		 */
		 private $id;
		
		/**
		 * Module id
		 * @var integer
		 */
		 private $moduleId;

		 /**
		 * Task name
		 * @var string
		 */
		 private $name;

		 /**
		 * Task Description
		 * @var string
		 */
		 private $description;

		 /**
		 * Task owner id
		 * @var string
		 */
		 private $ownerId;

		 /**
		 * Task assignee
		 * @var string
		 */
		 private $assigneeId;

		 /**
		 * Task startdate
		 * @var date
		 */
		 private $startDate;

		/**
		 * Task deadline
		 * @var date
		 */
		 private $deadline;

		 /**
		 * Task enddate
		 * @var date
		 */
		 private $endDate;

		 /**
		 * Task priority
		 * @var string
		 */
		 private $priority;

		/**
		 * Task status
		 * @var string
		 */
		 private $status;

		 /**
		 * Task percentage
		 * @var integer
		 */
		 private $percentage;


		 public function __construct($id)
		 {
			 parent::init();

			 $this->id=$id;

			 $data = self::$db->getDBContentsByCond("tms_task","id='$id'");

			 $this->moduleId = $data[0]['project_modules_id'];
			 $this->name = $data[0]['task_name'];
			 $this->description = $data[0]['task_description'];
			 $this->owner = $data[0]['owner_id'];
			 $this->assignee = $data[0]['assignee_id'];
			 $this->startDate = $data[0]['starting_date'];
			 $this->deadline = $data[0]['deadline'];
			 $this->endDate = $data[0]['ending_date'];
			 $this->priority = $data[0]['priority'];
			 $this->status = $data[0]['status'];
			 $this->percentage = $data[0]['percentage_completed'];

		 }
			
		 /**
		 * Function to get the module id
		 *return int module id 
		 */
		 public function getTaskModuleId()
		 {
			 return $this->moduleId;
		 }

		 /**
		 * Function to get the task name
		 *return varchar task name 
		 */
		 public function getTaskName()
		 {
			 return $this->name;
		 }

		 /**
		 * Function to get the task description
		 *return varchar task description 
		 */
		 public function getTaskDescription()
		 {
			 return $this->description;
		 }

		 /**
		 * Function to get the task owner
		 *return varchar task owner 
		 */
		 public function getTaskOwner()
		 {
			 return $this->owner;
		 }

		 /**
		 * Function to get the task assignee
		 *return varchar task assignee 
		 */
		 public function getTaskAssignee()
		 {
			 return $this->assignee;
		 }

		 /**
		 * Function to get the task start date
		 *return date task start date 
		 */
		 public function getTaskStartDate()
		 {
			 return $this->startDate;
		 }

		 /**
		 * Function to get the task deadline
		 *return date task deadline
		 */
		 public function getTaskDeadline()
		 {
			 return $this->deadline;
		 }

		 /**
		 * Function to get the task end date
		 *return date task end date
		 */
		 public function getTaskEndDate()
		 {
			 return $this->endDate;
		 }

		 /**
		 * Function to get the task priority
		 *return enum task priority
		 */
		 public function getTaskPriority()
		 {
			 return $this->priority;
		 }

		 /**
		 * Function to get the task status
		 *return enum task status
		 */
		 public function getTaskStatus()
		 {
			 return $this->status;
		 }

		  /**
		 * Function to get the task percentage
		 *return int task percentage
		 */
		 public function getTaskPercentage()
		 {
			 return $this->percentage;
		 }

		 /**
		 * Function to get the task comments
		 *return array comment id's
		 */
		 public function getComments()
		 {
			 $field = self::$db->getDBFieldByCond("tms_task_comments","id","task_id='$this->id'");
			 return $field;
		 }
		

		/**
		*Function to get number of days pending from current date till task deadline
		*return int number of days
		*/
		public function getDaysUntilDeadline()
		 {
			$field = self::$db->getDBFieldByCond("tms_task","deadline","id='$this->id'");
			$deadline =  $field[0];
			$deadline_arr = explode('-',$deadline);
			$deadline_timestamp = mktime(0,0,0,$deadline_arr[1],$deadline_arr[2],$deadline_arr[0]);
			
			$days = ($deadline_timestamp - mktime(0,0,0,date('m'),date('d'),date('Y')))/86400;
			return $days;
		 }


	   /**
		*
		* AddTask
		*
		* Function to add task
		* @param int $project_modules_id project modules id
		* @param string $task_name name of the task
		* @param int $owner_id id of the task creater
		* @param int $assignee_id  assignee id
		* @param date $starting_date starting date of the task
		* @param date $deadline deadline of the task
		* @param string $priority priority of that task
		* @param string $status status of that task
		* @param string $task_description description for that task
		* @param date $ending_date ending date of that task
		* @return boolean Returns TRUE on success or FALSE on failure
		*/
		
		public static function addTask ($project_modules_id,$task_name,$owner_id,$assignee_id,$starting_date,$deadline,$priority,$status,$task_description="NULL",$ending_date="NULL")
		 {
				parent::init();
				$fields="project_modules_id,task_name,task_description,owner_id,assignee_id,starting_date,deadline,ending_date,priority,status";
				$array=array("$project_modules_id","$task_name","$task_description","$owner_id","$assignee_id","$starting_date","$deadline","$ending_date","$priority","$status");

				$status = self::$db->dbInsert("tms_task",$fields,$array);

				if($status==0||$status=="Error Occured")
					return false;
				else 
					return true;
		 }


		/**
		 * Function to edit the task name
		 * @param string $newName  new task name
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */

		public function editName($newName)
		{
			$id=$this->id;
			$field="task_name ='$newName'  WHERE id = $id;";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		}
		/**
		 * Function to edit the task description
		 * @param string $newDescription  new task description
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function editDescription($newDescription)
		{
			$id=$this->id;
			$field="task_description ='$newDescription'  WHERE id = $id;";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		}
		/**
		 * Function to reassign a task to another user
		 * @param int $assigneeId  user id
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function reassign($assigneeId)
		{
			$id=$this->id;
			$field="assignee_id ='$assigneeId'  WHERE id = $id;";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		}

		/**
		 * Function to extend the deadline date
		 * @param date $extend  new deadline date
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		 public function extendDeadline($extend)
		{
			$id=$this->id;
			$field="deadline ='$extend'  WHERE id = $id;";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		}
		/**
		 * Function to change priority
		 * @param string $newPriority  
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function changePriority($newPriority)
		{
			$id=$this->id;
			$field="priority ='$newPriority'  WHERE id = $id;";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		}

		/**
		 * Function to change status
		 * @param string $newStatus  
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function changeStatus($newStatus)
		{
			$id=$this->id;
			$field="status ='$newStatus'  WHERE id = $id";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		}

		/**
		 * Function to change status of the task as completed and set ending date
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function completed()
		{
			$id=$this->id;
			$this->changeStatus("COMPLETED");
			$this->setPercentage('100');
			$date=date('Y-m-d');
			$field="ending_date='$date' WHERE id = $id";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		}

		/**
		 * Function to change status of the task as drop  
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function drop()
		{
			$id=$this->id;
			$result=$this->changeStatus("DROP");
			return $result;
		}

		/**
		 * Function to change status of the task as require assistance 
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function requireAssistance()
		{
			$id=$this->id;
			$result=$this->changeStatus("REQUIRE ASSISTANCE");
			return $result;
		}

		/**
		 * Function to change status of the task as open 
		 * @return boolean Returns TRUE on success or FALSE on failure
		 */
		public function open()
		{
			$id=$this->id;
			if($this->status=="CLOSED")
			{
				$result=$this->changeStatus("OPEN");
				 			
			return $result;
			}
		}


		/**
		 * Function to return status
		 * @return all status
		 */

		public static function getStatusFields()
		 {
			return $status="--STATUS--,CLOSED,OPEN,COMPLETED,DEFERRED,DROP,REQUIRE_ASSISTANCE";

		 }

		/**
		*setPercentage()
		*
		*A function to update the percentage in db
		*@param $percentage percentage for updation
		*@return boolean true on sucess and false on failure
		*/
		public function setPercentage($percentage)
		 {
			
			$field="percentage_completed ='$percentage'  WHERE id ='$this->id'";
			$result=self::$db->dbUpdate("tms_task",$field,$log=0);
			return $result;
		 }

	
	
 }//classEnd

?>